#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>


int main(int argc, char **argv)
{
	int				pipe_fd[2];
	pid_t		         	child_pid;

	char		        	buf[100];

	memset(buf,0,sizeof(buf)); 

	if(pipe(pipe_fd) < 0)//创建管道
	{
		printf("pipe creat failure：%s\n",strerror(errno));
                return -1;
	}
        printf("pipe creat successful\n");	
	
	if ((child_pid = fork()) == -1)//创建进程
	{
		printf("fork creat failure:%s\n",strerror(errno));
		return 0;
	}
        printf("fork creat successful\n");        

        if(child_pid == 0)
	{
                //子进程
                printf("child_pid start working...\n");
                int w_rite= 0;
                close(pipe_fd[0]);    //管道只能一读一写 ，0读 ，1写
                
                {
                      strcpy(buf, "hello world");
                      w_rite= write(pipe_fd[1],buf,strlen(buf)); //进行写操作
                   if (w_rite != -1)
                   {
                        printf("write \n");
                   }
                   else
                   {
                        printf("write error... \n");
                   }

      
                   close(pipe_fd[1]);  //关掉写操作	
               }
          
               
        }
        else 
        {   //父进程
              int read_len = 0;
              close(pipe_fd[1]);//关掉写端
        
        {
              read_len = read(pipe_fd[0], buf, 100);//读数据，读到怎么做，读不到怎么做等

              printf("p->child pid %d read len %d: %s\n", child_pid, read_len, buf);

              
         }
        close(pipe_fd[0]); 
 
        } 
        return 0;
}
                 

